package cn.com.dcsgo.service.impl;

import cn.com.dcsgo.mapper.RoleMapper;
import cn.com.dcsgo.model.acl.AdminRole;
import cn.com.dcsgo.model.acl.Role;
import cn.com.dcsgo.service.AdminRoleService;
import cn.com.dcsgo.service.RoleService;
import cn.com.dcsgo.vo.acl.RoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * RoleServiceImpl
 *
 * @author Dcsgo
 * @since 2025/3/18 14:05
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    private final AdminRoleService adminRoleService;

    @Override
    public IPage<Role> selectRolePage(Page<Role> pageParam, RoleQueryVo roleQueryVo) {
        //构造查询包装器
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        //获取查询条件值
        String roleName = roleQueryVo.getRoleName();
        //不为空进行条件分页查询
        if (!StringUtils.isEmpty(roleName)) {
            wrapper.like(Role::getRoleName, roleName);
        }
        return baseMapper.selectPage(pageParam, wrapper);
    }

    @Override
    public Map<String, Object> selectRolesAndAssigned(Long adminId) {
        Map<String, Object> result = new HashMap<>();
        //查询所有角色
        List<Role> roles = baseMapper.selectList(null);
        //查询用户已经分配的角色id
        LambdaQueryWrapper<AdminRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminRole::getAdminId, adminId);
        List<AdminRole> adminRoles = adminRoleService.list(wrapper);
        //通过adminRoles筛选出用户已经分配的角色id
        List<Long> roleIds = adminRoles.stream().map(AdminRole::getRoleId).collect(Collectors.toList());
        //将所有角色中是当前用户已经分配的角色筛选出来
        List<Role> assignRoles = new ArrayList<>(roles.size());
        for (Role role : roles) {
            if (roleIds.contains(role.getId())) {
                assignRoles.add(role);
            }
        }
        result.put("allRolesList", roles);
        result.put("assignRoles", assignRoles);
        return result;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public boolean saveAdminRoles(Long adminId, Long[] roleIds) {
        //先将用户的所有角色信息删除
        LambdaQueryWrapper<AdminRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminRole::getAdminId, adminId);
        //用户有分配了的角色才删除，没分配也没发儿删
        if(!adminRoleService.list(wrapper).isEmpty()){
            if (!adminRoleService.remove(wrapper)) {
                log.error("删除用户 id={},相关角色信息失败", adminId);
                return false;
            }
        }

        //再将新的角色信息批量添加到表中
        List<AdminRole> adminRoleList = new ArrayList<>(roleIds.length);
        for (Long roleId : roleIds) {
            AdminRole adminRole = new AdminRole();
            adminRole.setAdminId(adminId);
            adminRole.setRoleId(roleId);
            adminRoleList.add(adminRole);
        }
        if (!adminRoleService.saveBatch(adminRoleList)) {
            log.error("给用户 id={},添加新的角色信息失败", adminId);
        }
        return true;
    }
}
